package com.dllo.lanou.cookapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SimpleAdapter;


import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.activity.SearchActivity;
import com.dllo.lanou.cookapp.adapter.ClassifyNameAdapter;
import com.dllo.lanou.cookapp.entity.BookList;
import com.dllo.lanou.cookapp.entity.CookClassifyEntity;
import com.dllo.lanou.cookapp.view.PullToZoomListView;
import com.dllo.lanou.cookapp.volly.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xijinhua on 2016/3/28.
 */
public class ClassifyFragment extends Fragment {
@Bind(R.id.listView)
PullToZoomListView listView;
    List<CookClassifyEntity.ClassifyEntity> listEntities= new ArrayList<>();
    private ClassifyNameAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_classify,null);
        ButterKnife.bind(this,view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new ClassifyNameAdapter(getActivity());
        requestData();
        listView.getHeaderView().setImageResource(R.drawable.bc);
        listView.getHeaderView().setScaleType(ImageView.ScaleType.CENTER_CROP);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                position=position-listView.getHeaderViewsCount();
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                long ids = listEntities.get(position).getId();
                intent.putExtra("name",listEntities.get(position).getTitle());
                intent.putExtra("classify",true);
                intent.putExtra("id",ids);
                startActivity(intent);
            }
        });

    }


    //请求数据
    public void requestData() {
        String path = "http://www.tngou.net/api/cook/classify";
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("分类的的数据", response);
                Gson gson = new Gson();
                CookClassifyEntity cookClassifyEntity = gson.fromJson(response,CookClassifyEntity.class);
                if (cookClassifyEntity != null){
                    listEntities = cookClassifyEntity.getTngou();
                    adapter.addDatas(listEntities);

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "4ee0087cd4103e3acbab0f8efbf0bbd1");
                return map;
            }


        };
        VolleySingleton.getInstance().addRequest(request);
    }
}
